import 'dart:convert';

import 'package:cash_drawer/common/common.dart';
import 'package:cash_drawer/common/user.dart';
import 'package:cash_drawer/utils/utils.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'apis.dart';
import 'dio_manager.dart';
import 'model/user_model.dart';

ApiService _apiService = new ApiService();

ApiService get apiService => _apiService;

class ApiService {
  Options _getOptions() {
    Map<String, String> map = new Map();
    map["Cookie"] = User().cookie.toString();
    return Options(headers: map);
  }

  /// 登录
  void login(Function callback, Function errorCallback, String _username, String _password) async {

    FormData formData = new FormData.fromMap({"username": _username, "password": _password});
    dio.post(Apis.LOGIN_PARAM, data: formData).then((response) {
      callback(UserModel.fromJson(response.data), response);
    }).catchError((e) {
      errorCallback(e);
    });
  }


  void getKey(Function callback, Function errorCallback) async {
    String appSn = AppConfig.SN;
    var _newDate = DateTime.now();
    String txt = "appSn="+appSn+"&"+"randomStr="+ _newDate.toString()+AppConfig.KEY_SAAS_MD5;
    String md5Txt = Utils.generateMd5(txt);
    dio.post(Apis.GET_KEY, data: {"systemType": AppConfig.SYSTEM_TYPE,
      "oprByPad": "fase",
      "sign":md5Txt,"randomStr":_newDate.toString(),
      "version": "2.0.2"}).
    then((response) {
      callback(response.data, response);
    }).catchError((e) {
      errorCallback(e);
    });
  }


//  /// 获取收藏列表
//  void getCollectionList(
//      Function callback, Function errorCallback, int _page) async {
//    dio.get(Apis.COLLECTION_LIST + "/$_page/json").then((response) {
//      callback(CollectionModel.fromJson(response.data));
//    }).catchError((e) {
//      errorCallback(e);
//    });
//  }



//  /// 退出登录
//  void logout(Function callback, Function errorCallback) async {
//    dio.get(Apis.USER_LOGOUT).then((response) {
//      callback(BaseModel.fromJson(response.data));
//    }).catchError((e) {
//      errorCallback(e);
//    });
//  }

//
//  /// 获取TODO列表数据
//  void getTodoList(Function callback, Function errorCallback, int _page) async {
//    dio.get(Apis.TODO_LIST + "/$_page/json").then((response) {
//      callback(TodoListModel.fromJson(response.data));
//    }).catchError((e) {
//      errorCallback(e);
//    });
//  }




}
